package com.assessment.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.assessment.dao.AssessmentProjectRepository;
import com.assessment.dao.AssessmentRecordRepository;
import com.assessment.entity.AssessmentProject;
import com.assessment.entity.AssessmentRecord;
import com.assessment.entity.AssessmentTemplate;
import com.common.util.AJAXResultMessage;
import com.staff.dao.StaffRepository;
import com.staff.entity.Staff;
import com.staff.entity.dto.StaffQueryDTO;

@Service
@Transactional
public class AssessmentRecordService implements IAssessmentRecordService {

	@Autowired
	private AssessmentRecordRepository assessmentRecordRepository;
	@Autowired
	private StaffRepository staffRepository;
	@Autowired
	private AssessmentProjectRepository assessmentProjectRepository;
	
	
	@Override
	public AJAXResultMessage saveOrUpdate(AssessmentRecord assessmentRecord) {
		// TODO Auto-generated method stub
		Long staffId=assessmentRecord.getStaffId();
		Long projectId=assessmentRecord.getAssessmentProjectId();
		if(staffId==null||projectId==null) {
			return new AJAXResultMessage(false,"用户或者项目为空");
		}
		AssessmentRecord record=assessmentRecordRepository.findRecordByStaffIdAndProjectId(staffId,projectId);
		if(record!=null) {
			return new AJAXResultMessage(false,"你已经参与过考核");
		}
		else {
			assessmentRecord.setTime(new Date());
			Staff staff=staffRepository.findOne(staffId);
			AssessmentProject assessmentProject=assessmentProjectRepository.findOne(projectId);
			assessmentRecord.setStaff(staff);
			assessmentRecord.setAssessmentProject(assessmentProject);
			assessmentRecordRepository.save(assessmentRecord);
		}
		return new AJAXResultMessage(true,"考核成功");
	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub
		AssessmentRecord assessmentRecord=assessmentRecordRepository.findOne(id);
		if(assessmentRecord!=null) {
			assessmentRecord.setAssessmentProject(null);
			assessmentRecord.setStaff(null);
			assessmentRecordRepository.save(assessmentRecord);
			assessmentRecordRepository.delete(assessmentRecord);
		}		
	}

	@Override
	public void delete(List<Long> ids) {
		// TODO Auto-generated method stub
		List<AssessmentRecord> assessmentRecordList=(List<AssessmentRecord>) assessmentRecordRepository.findAll(ids);
		if(assessmentRecordList!=null) {
			for(AssessmentRecord assessmentRecord :assessmentRecordList) {
				assessmentRecord.setAssessmentProject(null);
				assessmentRecord.setStaff(null);
				assessmentRecordRepository.save(assessmentRecord);
			}
			assessmentRecordRepository.delete(assessmentRecordList);
		}	
	}

	@Override
	@Transactional(readOnly=true)
	public AssessmentRecord findOne(Long id) {
		// TODO Auto-generated method stub
		return assessmentRecordRepository.findOne(id);
	}

	@Override
	@Transactional(readOnly=true)
	public List<AssessmentRecord> findAll() {
		// TODO Auto-generated method stub
		return (List<AssessmentRecord>) assessmentRecordRepository.findAll();
	}

	@Override
	@Transactional(readOnly=true)
	public Page<AssessmentRecord> findAll(AssessmentRecord assessmentRecord, Pageable pageable) {
		// TODO Auto-generated method stub
		if(assessmentRecord.getStaffName()!=null) {
			System.out.println("第一个if");
			StaffQueryDTO staffDTO=new StaffQueryDTO();
			staffDTO.setStaffName(assessmentRecord.getStaffName());
			List<Staff> staffList=staffRepository.findAll(StaffQueryDTO.getSpecification(staffDTO));
			Staff staff=staffList.get(0);
			System.out.println("第一个staff+"+staff);
			assessmentRecord.setStaff(staff);
		}
		if(assessmentRecord.getAssessmentProjectName()!=null) 
		{
			System.out.println("第2个if");
			AssessmentProject assessmentProject=new AssessmentProject();
			assessmentProject.setAssessmentTemplateName(assessmentRecord.getAssessmentProjectName());
			List<AssessmentProject> assessmentProjectList=assessmentProjectRepository.findAll(AssessmentProject.getSpecification(assessmentProject));
			assessmentProject=assessmentProjectList.get(0);
			System.out.println("第一个assessmentProject+"+assessmentProject);
			assessmentRecord.setAssessmentProject(assessmentProject);
		}
		Page<AssessmentRecord> page=assessmentRecordRepository.findAll(AssessmentRecord.getSpecification(assessmentRecord),pageable);
		List<AssessmentRecord> list=page.getContent();
		for(int i=0;i<list.size();i++) {
			Long assessmentProjectId= list.get(i).getAssessmentProject().getId();
			String assessmentProjectName=list.get(i).getAssessmentProject().getName();
			Long staffId= list.get(i).getStaff().getId();
			String staffName=list.get(i).getStaff().getStaffName();
			//Long assessmentTemplateId=list.get(i).getAssessmentProject().getAssessmentTemplate().getId();	
			//String assessmentTemplateName=list.get(i).getAssessmentProject().getAssessmentTemplate().getName();
			list.get(i).setAssessmentProjectId(assessmentProjectId);
			list.get(i).setAssessmentProjectName(assessmentProjectName);
			
			list.get(i).setStaffId(staffId);
			list.get(i).setStaffName(staffName);
		}
		Page<AssessmentRecord> page1 = new PageImpl<AssessmentRecord>(list,pageable, page.getTotalElements());
		return page1;
	}

}
